﻿
using LuxsanCommunicationTool.Communication;
using LuxsanTool.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using static LuxsanBaseHelpLib.LibBackMsg.LibBackMessage;
using System.Threading;
namespace LuxsanTool
{
    public class MachineWorkManage
    {

        /// <summary>
        /// 文件路径
        /// </summary>
        public static string m_strFilePath = Application.StartupPath + "\\Config\\ProceeConfig\\MachineWorkManage";

        /// <summary>
        /// 保存文件
        /// </summary>
        public static LuxsanBaseHelpLib.FileOperate.IniFile m_iniFile = new LuxsanBaseHelpLib.FileOperate.IniFile();

        /// <summary>
        /// 暂停
        /// </summary>
        public static bool m_bPause = true;
        /// <summary>
        /// 开始
        /// </summary>
        public static bool m_bStart = false;

        public static bool m_bPauseHive = false;

        /// <summary>
        /// 运行模式
        /// </summary>
        public static Int16 m_iRunMode = 1;

        public static string m_strRunMode = "PRO";

        public static int m_iCurrentCpkCount = 0;

        public static int m_iCurrentGrrCount = 0;

   

        /// <summary>
        /// Hive 机器状态
        /// 1 running
        /// 2 idel
        /// 5 down
        /// 100 com err
        /// </summary>
        public static Int16 m_iMachineState =5;

        /// <summary>
        /// 周保养
        /// </summary>
        public static Int32 m_WeeklyMaintenance = 7;
        /// <summary>
        /// 月保养
        /// </summary>
        public static Int32 m_MonthlyMaintenance = 30;

        public static string m_strWeeklyMaintenanceTime;//周保养时间
        public static string m_strMonthlyMaintenanceTime;//月保养时间

        public static bool m_boffBoard;

        //public static bool m_bMacLeftSN;

        //public static bool m_bMacRightSN;



        /// <summary>
        /// 工站存储
        /// </summary>
        public static Dictionary<string , InstanceStationWork> m_stationWork = new Dictionary<string, InstanceStationWork>();

        public static void SetFilePath(string _strFilePath)
        {
            m_strFilePath = _strFilePath + "\\Config\\ProceeConfig\\MachineWorkManage";
        }


        //public static bool bMacLeftSN 
        //{
        //    get { return m_bMacLeftSN; } // getter直接返回组合值
        //    set // 使用this关键字正确引用当前实例的firstName字段
        //    {
        //        m_bMacLeftSN = value;
        //        SaveMaintenance();
        //    } 
        //}

        //public static bool bMacRightSN 
        //{
        //    get { return m_bMacRightSN; } // getter直接返回组合值
        //    set // 使用this关键字正确引用当前实例的firstName字段
        //    {
        //        m_bMacRightSN = value;
        //        SaveMaintenance() ;
        //    } 
        //}




        #region Updata 更新连线
        //public static void Updata(string _strStationName)
        //{
        //    if (!MachineWorkManage.m_stationWork.Keys.Contains(_strStationName))
        //        return;
        //    MachineWorkManage.m_stationWork[_strStationName].m_strLogName = _strStationName;
        //    //所以LOG都记录到主流程
        //    foreach (KeyValuePair<string, Tool.BaseTool> tool in MachineWorkManage.m_stationWork[_strStationName].m_mesBaseTool)
        //    {
        //        tool.Value.m_strStationName = _strStationName;  
        //        tool.Value.m_strLogName = _strStationName;

        //        if (tool.Value.m_EditId == (int)Tool.ToolEnum.ToolEditEnum.判断工具)
        //        {

        //            Tool.JudgeTool judgeTool = tool.Value as Tool.JudgeTool;
        //            foreach (KeyValuePair<string, Tool.Judge.JudgeToolConfig> judgeToolConfig in judgeTool.m_Judge)
        //            {

        //                string strExecuteTask1 = judgeToolConfig.Value.strExecuteTask1;
        //                Updata1(strExecuteTask1, _strStationName);


        //                string strExecuteTask2 = judgeToolConfig.Value.strExecuteTask2;
        //                Updata1(strExecuteTask2, _strStationName);

        //            }
        //        }
        //    }
        //}
        #endregion

        #region Updata1  更新连线
        //public static void Updata1(string _strStarionName, string _strLogName)
        //{
        //    MachineWorkManage.m_stationWork[_strStarionName].m_strLogName = _strLogName;
        //    foreach (KeyValuePair<string, Tool.BaseTool> tool in MachineWorkManage.m_stationWork[_strStarionName].m_mesBaseTool)
        //    {
        //        tool.Value.m_strStationName = _strStarionName;
        //        tool.Value.m_strLogName = _strLogName;
        //        if (tool.Value.m_EditId == (int)Tool.ToolEnum.ToolEditEnum.判断工具)
        //        {
        //            Tool.JudgeTool judgeTool = tool.Value as Tool.JudgeTool;
        //            foreach (KeyValuePair<string, Tool.Judge.JudgeToolConfig> judgeToolConfig in judgeTool.m_Judge)
        //            {
        //                string strExecuteTask1 = judgeToolConfig.Value.strExecuteTask1;
        //                Updata1(strExecuteTask1, _strLogName);

        //                string strExecuteTask2 = judgeToolConfig.Value.strExecuteTask2;
        //                Updata1(strExecuteTask2, _strLogName);
        //            }
        //        }
        //    }
        //}
        #endregion

        #region  Initial 初始化
        /// <summary>
        /// 初始化 包含加载工站 
        /// </summary>
        public static void Initial(LuxsanStationWorkDelegate _LuxsanStationWorkDelegate)
        {
            //List<string> listKeys =g_stationWork.Keys.ToList();
            //List<InstanceStationWork> listStationWork = g_stationWork.Values.ToList();

            LoadMaintenance();

            Task.Run(() =>
           {
               while (true)
               {
                   string strCurTime = DateTime.Now.ToString("yyyyMMdd");
                   if (strCurTime != m_strWeeklyMaintenanceTime)
                   {
                       m_strWeeklyMaintenanceTime = strCurTime;
                       m_WeeklyMaintenance--;
                       SaveMaintenance();
                   }
                   if (strCurTime != m_strMonthlyMaintenanceTime)
                   {
                       m_strMonthlyMaintenanceTime = strCurTime;
                       m_MonthlyMaintenance--;
                       SaveMaintenance();
                   }

                   System.Threading.Thread.Sleep(10000);
               }
             
           });
                //加载工站
           LoadStation(_LuxsanStationWorkDelegate);
           
        }
        #endregion

        #region  周月保养
        public static void SaveMaintenance()
        {
            if (!(System.IO.Directory.Exists(m_strFilePath)))
                System.IO.Directory.CreateDirectory(m_strFilePath);
            m_iniFile.WtiteString("Maintenance", "WeeklyMaintenanceTime", m_strWeeklyMaintenanceTime, m_strFilePath + "\\Sation.ini");
            m_iniFile.WtiteString("Maintenance", "MonthlyMaintenanceTime", m_strMonthlyMaintenanceTime, m_strFilePath + "\\Sation.ini");
            m_iniFile.WtiteString("Maintenance", "WeeklyMaintenance", m_WeeklyMaintenance.ToString(), m_strFilePath + "\\Sation.ini");
            m_iniFile.WtiteString("Maintenance", "MonthlyMaintenance", m_MonthlyMaintenance.ToString(), m_strFilePath + "\\Sation.ini");

            //m_iniFile.WtiteString("MacSN", "bMacLeftSN", m_bMacLeftSN.ToString(), m_strFilePath + "\\Sation.ini");
            //m_iniFile.WtiteString("MacSN", "bMacRightSN", m_bMacRightSN.ToString(), m_strFilePath + "\\Sation.ini");

        }
        public static void LoadMaintenance()
        {
            if (!(System.IO.Directory.Exists(m_strFilePath)))
                System.IO.Directory.CreateDirectory(m_strFilePath);
            m_strWeeklyMaintenanceTime= m_iniFile.GetString("Maintenance", "WeeklyMaintenanceTime", m_strFilePath + "\\Sation.ini");
            m_strMonthlyMaintenanceTime= m_iniFile.GetString("Maintenance", "MonthlyMaintenanceTime", m_strFilePath + "\\Sation.ini");
            m_WeeklyMaintenance= m_iniFile.GetInt("Maintenance", "WeeklyMaintenance",  m_strFilePath + "\\Sation.ini");
            m_MonthlyMaintenance= m_iniFile.GetInt("Maintenance", "MonthlyMaintenance", m_strFilePath + "\\Sation.ini");

            //m_bMacLeftSN = m_iniFile.GetBool("MacSN",false, "bMacLeftSN", m_strFilePath + "\\Sation.ini");
            //m_bMacRightSN = m_iniFile.GetBool("MacSN", false, "bMacRightSN", m_strFilePath + "\\Sation.ini");

        }
        #endregion

        #region SaveSingleStation  保存工站-单工站
        /// <summary>
        /// 保存工站-单工站
        /// </summary>
        public static void SaveSingleStation(string _strCurstationName, LuxsanToolDelegate _luxsanToolDelegate, LuxsanToolAlarmDelegate _LuxsanToolAlarmDelegate)
        {
            //if (System.IO.File.Exists(strFilePath + "\\Sation.ini"))
            //    System.IO.File.Delete(strFilePath + "\\Sation.ini");

            List<string> listKeys = m_stationWork.Keys.ToList();
            List<InstanceStationWork> listStationWork = m_stationWork.Values.ToList();

            // m_iniFile.WtiteString("WorkStationCount", "Count", listStationWork.Count.ToString(),m_strFilePath + "\\Sation.ini");

            for (int i = 0; i < listStationWork.Count; i++)
            {
                string strStationName = listStationWork[i].strStationName;
                if (_strCurstationName == strStationName)
                {
                    if (!(System.IO.Directory.Exists(m_strFilePath + "\\" + strStationName)))
                        System.IO.Directory.CreateDirectory(m_strFilePath + "\\" + strStationName);

                    m_iniFile.WtiteString("WorkStation", "bLogEnable", listStationWork[i].bLogEnable.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");
                    m_iniFile.WtiteString("WorkStation", "StationName", listStationWork[i].strStationName.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");
                    m_iniFile.WtiteString("WorkStation", "Enable", listStationWork[i].bEnable.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");

                    listStationWork[i].SaveTool(_strCurstationName, _luxsanToolDelegate, _LuxsanToolAlarmDelegate);
                }
            }
        }
        #endregion

        #region SaveStation  保存工站
        /// <summary>
        /// 保存工站
        /// </summary>
        public static void SaveStation(string _strCurstationName, LuxsanToolDelegate _luxsanToolDelegate, LuxsanToolAlarmDelegate _LuxsanToolAlarmDelegate)
        {
            //if (System.IO.File.Exists(strFilePath + "\\Sation.ini"))
            //    System.IO.File.Delete(strFilePath + "\\Sation.ini");

            List<string> listKeys = m_stationWork.Keys.ToList();
            List<InstanceStationWork> listStationWork = m_stationWork.Values.ToList();

            // m_iniFile.WtiteString("WorkStationCount", "Count", listStationWork.Count.ToString(),m_strFilePath + "\\Sation.ini");
           
                for (int i = 0; i < listStationWork.Count; i++)
                {
                    string strStationName = listStationWork[i].strStationName;
                    if (!(System.IO.Directory.Exists(m_strFilePath + "\\" + strStationName)))
                        System.IO.Directory.CreateDirectory(m_strFilePath + "\\" + strStationName);

                    m_iniFile.WtiteString("WorkStation", "bLogEnable", listStationWork[i].bLogEnable.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");
                    m_iniFile.WtiteString("WorkStation", "StationName", listStationWork[i].strStationName.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");

                    m_iniFile.WtiteString("WorkStation", "Enable", listStationWork[i].bEnable.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");
                   // m_iniFile.WtiteString("WorkStation", "bAsyEnable", listStationWork[i].bAsyEnable.ToString(), m_strFilePath + "\\" + strStationName + "\\Sation.ini");

                
                    listStationWork[i].SaveTool(_strCurstationName, _luxsanToolDelegate, _LuxsanToolAlarmDelegate);

                }
        
        }
        #endregion

        #region  LoadStation 加载工站
        /// <summary>
        /// LoadStation 加载工站
        /// </summary>
        public static void LoadStation(LuxsanStationWorkDelegate _LuxsanStationWorkDelegate)
        {
            if (!(System.IO.Directory.Exists(m_strFilePath)))
                System.IO.Directory.CreateDirectory(m_strFilePath);


            DirectoryInfo directoryInfo = new DirectoryInfo(m_strFilePath);
            DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();

            //Task[] tasks = new Task[directoryInfos.Length];
            //int iCount= m_iniFile.GetInt("WorkStationCount", "Count", m_strFilePath + "\\Sation.ini");
            for (int i = 0; i < directoryInfos.Length; i++)
            {
                string DirName = directoryInfos[i].Name;

                //  LuxsanProjectManage.Global.GetMainForm().Invoke(new MethodInvoker(delegate
                // {

                //tasks[i] = Task.Run(() =>
                //{
                    string strStationName = m_iniFile.GetString("WorkStation", "StationName", m_strFilePath + "\\" + DirName + "\\Sation.ini");
                    bool bEnable = m_iniFile.GetBool("WorkStation", false, "Enable", m_strFilePath + "\\" + DirName + "\\Sation.ini");
                    bool bLogEnable = m_iniFile.GetBool("WorkStation", false, "bLogEnable", m_strFilePath + "\\" + DirName + "\\Sation.ini");
                    bool bAsyEnable = m_iniFile.GetBool("WorkStation", false, "bAsyEnable", m_strFilePath + "\\" + DirName + "\\Sation.ini");

                InstanceStationWork _InstanceStationWork = new InstanceStationWork();
                    _InstanceStationWork.strStationName = strStationName;
                    _InstanceStationWork.bEnable = bEnable;
                    _InstanceStationWork.bLogEnable = bLogEnable;
                   // _InstanceStationWork.bAsyEnable = bAsyEnable;

                //工站初始化

                _InstanceStationWork.SetFilePath(m_strFilePath, LuxsanProjectManage.Global.GetSystemPara().m_strLogFilePath);
                    _InstanceStationWork.Initial();
                    _InstanceStationWork.strStationName = strStationName;
                    _InstanceStationWork.bEnable = bEnable;
                    _InstanceStationWork.bLogEnable = bLogEnable;
                //_InstanceStationWork.bAsyEnable = bAsyEnable;
                if (strStationName != "0")
                        AddStation(strStationName, _InstanceStationWork, _LuxsanStationWorkDelegate);
               // });

                // tasks[i].SetApartmentState(ApartmentState.STA);
                // tasks[i].Start();
                // tasks[i].Join();
                //}));
            }
           // Task.WhenAll(tasks);

        }
        #endregion


        #region SaveStation  保存工站
        ///// <summary>
        ///// 保存工站
        ///// </summary>
        ///// <param name="_stationName"></param>
        ///// <param name="_luxsanToolDelegate"></param>
        ///// <param name="_LuxsanToolAlarmDelegate"></param>

        //public static void SaveStation(string _stationName, LuxsanToolDelegate _luxsanToolDelegate, LuxsanToolAlarmDelegate _LuxsanToolAlarmDelegate)
        //{
        //    if (!(System.IO.Directory.Exists(m_strFilePath)))
        //        System.IO.Directory.CreateDirectory(m_strFilePath);

        //    //if (System.IO.File.Exists(strFilePath + "\\Sation.ini"))
        //    //    System.IO.File.Delete(strFilePath + "\\Sation.ini");


        //    List<string> listKeys = m_stationWork.Keys.ToList();
        //    List<InstanceStationWork> listStationWork = m_stationWork.Values.ToList();

        //    m_iniFile.WtiteString("WorkStationCount", "Count", listStationWork.Count.ToString(), m_strFilePath + "\\Sation.ini");

        //    for (int i = 0; i < listStationWork.Count; i++)
        //    {


        //        m_iniFile.WtiteString("WorkStation_" + i.ToString(), "bLogEnable", listStationWork[i].bLogEnable.ToString(), m_strFilePath + "\\Sation.ini"); ;
        //        m_iniFile.WtiteString("WorkStation_" + i.ToString(), "StationName", listStationWork[i].strStationName.ToString(), m_strFilePath + "\\Sation.ini");
        //        m_iniFile.WtiteString("WorkStation_" + i.ToString(), "Enable", listStationWork[i].bEnable.ToString(), m_strFilePath + "\\Sation.ini");

        //        listStationWork[i].SaveTool(_stationName, _luxsanToolDelegate, _LuxsanToolAlarmDelegate);

        //    }
        //}
        #endregion

        #region  LoadStation 加载工站
        ///// <summary>
        ///// LoadStation 加载工站
        ///// </summary>
        //public static void LoadStation(LuxsanStationWorkDelegate _LuxsanStationWorkDelegate)
        //{


        //    if (!(System.IO.Directory.Exists(m_strFilePath)))
        //        System.IO.Directory.CreateDirectory(m_strFilePath);

        //    //DirectoryInfo directoryInfo = new DirectoryInfo(m_strFilePath);
        //    //DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
        //    int iCount = m_iniFile.GetInt("WorkStationCount", "Count", m_strFilePath + "\\Sation.ini");
        //    for (int i = 0; i < iCount; i++)
        //    {
        //        //string DirName = directoryInfos[i].Name;
        //        // Task.Run(() =>
        //        //{
        //        string strStationName = m_iniFile.GetString("WorkStation_" + i.ToString(), "StationName", m_strFilePath + "\\Sation.ini");

        //        bool bEnable = m_iniFile.GetBool("WorkStation_" + i.ToString(), false, "Enable", m_strFilePath + "\\Sation.ini");
        //        bool bLogEnable = m_iniFile.GetBool("WorkStation_" + i.ToString(), false, "bLogEnable", m_strFilePath + "\\Sation.ini");

        //        InstanceStationWork _InstanceStationWork = new InstanceStationWork();
        //        _InstanceStationWork.strStationName = strStationName;
        //        _InstanceStationWork.bEnable = bEnable;
        //        _InstanceStationWork.bLogEnable = bLogEnable;
        //        //工站初始化
        //        _InstanceStationWork.SetFilePath(m_strFilePath, LuxsanProjectManage.Global.GetSystemPara().m_strLogFilePath);
        //        _InstanceStationWork.Initial();
        //        AddStation(strStationName, _InstanceStationWork, _LuxsanStationWorkDelegate);
        //        //});
        //    }

        //    //List<InstanceStationWork> listStationWork = m_stationWork.Values.ToList();
        //    //for (int i = 0; i < listStationWork.Count; i++)
        //    //{
        //    //    Updata(listStationWork[i].strStationName);
        //    //}
        //}
        #endregion

        #region  CopyStation  Copy工站
        /// <summary>
        /// CopyStation Copy工站
        /// </summary>
        public static InstanceStationWork CopyStation(string _strCurStationName,LuxsanStationWorkDelegate _LuxsanStationWorkDelegate)
        {
            InstanceStationWork _InstanceStationWork = null;

            if (!(System.IO.Directory.Exists(m_strFilePath)))
                System.IO.Directory.CreateDirectory(m_strFilePath);


            DirectoryInfo directoryInfo = new DirectoryInfo(m_strFilePath);
            DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
            //int iCount = m_iniFile.GetInt("WorkStationCount", "Count", m_strFilePath +"\\"+_strCurStationName +"\\Sation.ini");
            for (int i = 0; i < directoryInfos.Length; i++)
            {
                string strStationName = m_iniFile.GetString("WorkStation" , "StationName", m_strFilePath + "\\" + _strCurStationName + "\\Sation.ini");
                bool  bEnable = m_iniFile.GetBool("WorkStation",false, "Enable", m_strFilePath + "\\" + _strCurStationName + "\\Sation.ini");
              
                if (strStationName == _strCurStationName)
                {
                    _InstanceStationWork = new InstanceStationWork();
                    _InstanceStationWork.strStationName = strStationName;
                    _InstanceStationWork.bEnable = bEnable;
                    //工站初始化
                    _InstanceStationWork.SetFilePath(m_strFilePath, LuxsanProjectManage.Global.GetSystemPara().m_strLogFilePath);
                    _InstanceStationWork.Initial();

                    break;
                }
            }

            return _InstanceStationWork;
        }
        #endregion

        #region   SetLuxsanDelegate 设定委托
        public static void SetLuxsanDelegate(LuxsanTool.Tool.LuxsanToolDelegate _luxsanToolRunningDelegate, LuxsanTool.Tool.LuxsanToolAlarmDelegate _luxsanToolAlarmDelegate)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                foreach (KeyValuePair<string,BaseTool> basetool in  instanceStationWork.Value.m_mesBaseTool)
                {
                    basetool.Value.m_LuxsanToolDelegate = _luxsanToolRunningDelegate;
                    basetool.Value.m_LuxsanToolAlarmDelegate = _luxsanToolAlarmDelegate;
                }
            }
        }
       

        public static void SetLuxsanToolDelegate(string _strStationName ,string _strToolName, LuxsanTool.Tool.LuxsanToolDelegate _luxsanToolRunningDelegate)
        {
            m_stationWork[_strStationName].m_mesBaseTool[_strToolName].m_LuxsanToolDelegate = _luxsanToolRunningDelegate;
        }

        public static void SetLuxsanToolAlarmDelegate(string _strStationName, string _strToolName, LuxsanTool.Tool.LuxsanToolAlarmDelegate _luxsanToolAlarmDelegate)
        {
            m_stationWork[_strStationName].m_mesBaseTool[_strToolName].m_LuxsanToolAlarmDelegate = _luxsanToolAlarmDelegate;
        }
        #endregion




        #region  AddStation  添加工站
        /// <summary>
        /// 添加工站
        /// </summary>
        /// <param name="_strStationNmae"></param>
        /// <param name="_StationWork"></param>
        /// <returns></returns>
        public static LuxsanBaseHelpLib.ExecuteStatus AddStation(string _strStationNmae, InstanceStationWork _StationWork, LuxsanStationWorkDelegate _LuxsanStationWorkDelegate)
        {
            LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();

            if (!m_stationWork.Keys.Contains(_strStationNmae))
            {
                
                m_stationWork.Add(_strStationNmae, _StationWork);
                _StationWork.strStationName = _strStationNmae;

                _StationWork.luxsanStationWorkDelegate = _LuxsanStationWorkDelegate;

                executeStatus.bStatus = true;
                executeStatus.strErrorMsg = "";
            }
            else
            {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "Work is Existed ";
            }

           return executeStatus;
        }
        #endregion



        #region  ModifyStation 调整工站
        /// <summary>
        /// 调整工站
        /// </summary>
        /// <param name="_strStationNmae"></param>
        /// <param name="_StationWork"></param>
        /// <returns></returns>
        public static LuxsanBaseHelpLib.ExecuteStatus ModifyStation(string _strNewName, string _strOldName)
        {
            LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();

            if (m_stationWork.Keys.Contains(_strOldName)&&!m_stationWork.Keys.Contains(_strNewName))
            {
                m_stationWork = m_stationWork.ToDictionary(k => k.Key == _strOldName ? _strNewName : k.Key, k => k.Value);

                m_stationWork[_strNewName].strStationName = _strNewName;

                m_stationWork[_strNewName].luxsanStationWorkDelegate("DeleteStation","", _strNewName, "");

                try
                {
                    if (Directory.Exists((m_strFilePath + "\\" + _strOldName)))
                        Directory.Delete(m_strFilePath + "\\" + _strOldName, true);
                }
                catch { }

                executeStatus.bStatus = true;
                executeStatus.strErrorMsg = "";
            }
            else
            {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "新工站名称或旧工站名称  is Existed ";
            }

            return executeStatus;
        }
        #endregion

        #region DeleteStation 删除工站
        /// <summary>
        /// 删除工站
        /// </summary>
        /// <param name="_strStationName"></param>
        /// <returns></returns>
        public static LuxsanBaseHelpLib.ExecuteStatus DeleteStation(string _strStationName)
        {
            LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();
            if (m_stationWork.Keys.Contains(_strStationName))
            {
                //先释放工站
                StopWork(_strStationName);
                //删除工站
                m_stationWork.Remove(_strStationName);

                if (Directory.Exists((m_strFilePath + "\\" + _strStationName)))
                    Directory.Delete(m_strFilePath + "\\" + _strStationName, true);

                executeStatus.bStatus = true;
                executeStatus.strErrorMsg = "";
            }
            else
            {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "Work is  No Existed ";
            }
            return executeStatus;
        }
        #endregion



        #region  ModifyConfigStationName  调整引用工站名(调整工站名的时候调用)
        /// <summary>
        /// 调整引用工站名(调整工站名的时候调用)
        /// </summary>
        public static void ModifyConfigStationName(string _strNewStationName, string _strOldStationName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.ModifyStationConfigName(_strNewStationName, _strOldStationName);
              
            }
        }
        #endregion

        #region  DeleteConfigStationName删除引用工站名(调整工站名的时候调用)
        /// <summary>
        /// 删除引用工站名(调整工站名的时候调用)
        /// </summary>
        public static void DeleteConfigStationName(string _strDeleteStationName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.DeleteStationConfigName(_strDeleteStationName);
            }
        }
        #endregion

        #region  ModifyConfigToolName 调整引用工具名(调整工具名的时候调用)
        /// <summary>
        ///调整引用工具名(调整工具名的时候调用)
        /// </summary>
        public static void ModifyConfigToolName(string _strCurrentStation, string _strNewToolName, string _strOldToolName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.ModifyToolConfigName(_strCurrentStation,_strNewToolName, _strOldToolName);
            }
        }
        #endregion

        #region DeleteConfigToolName 删除引用工具名 (调整工具名的时候调用)
        /// <summary>
        /// 删除引用工具名 (调整工具名的时候调用)
        /// </summary>
        public static void DeleteConfigToolName(string _strCurrentStation, string _strDeleteToolName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.DeleteToolConfigName(_strCurrentStation,_strDeleteToolName);
            }
        }
        #endregion

        #region ModifyConfigComName 调整引用通讯名 (调整通讯名的时候调用)
        /// <summary>
        /// 调整引用通讯名 (调整通讯名的时候调用)
        /// </summary>
        public static void ModifyConfigComName(string _strNewComName, string _strOldComName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.ModifyComConfigName(_strNewComName, _strOldComName);
            }
        }
        #endregion

        #region  DeleteConfigComName  删除引用通讯名 (调整通讯名的时候调用)
        /// <summary>
        ///  删除引用通讯名 (调整通讯名的时候调用)
        /// </summary>
        public static void DeleteConfigComName(string _strDeleteComName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.DeleteComConfigName(_strDeleteComName);
            }
        }
        #endregion

        #region ModifyConfigValueName 调整引用获取值(输出值改变)   反调引用
        /// <summary>
        /// 调整引用获取值(输出值改变)   反调引用
        /// </summary>
        public static void ModifyConfigValueName(string _strCurStation, string _strCurTool, string _strNewValueName, string _strOldValueName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.ModifyValueConfigName(_strCurStation, _strCurTool,_strNewValueName, _strOldValueName);
            }
        }
        #endregion

        #region  DeleteConfigValueName 删除引用获取值(输出值改变)   反调引用
        /// <summary>
        /// 删除引用获取值(输出值改变)   反调引用
        /// </summary>
        public static void DeleteConfigValueName(string _strDeleteValueName)
        {
            foreach (KeyValuePair<string, InstanceStationWork> instanceStationWork in m_stationWork)
            {
                instanceStationWork.Value.DeleteValueConfigName(_strDeleteValueName);
            }
        }
        #endregion



      

        #region StartWork 开始运行所有工站
        /// <summary>
        /// 开始运行所有工站
        /// </summary>
        public static void StartWork()
        {
            foreach(var Item in m_stationWork)
            {
                Item.Value.bPause = false;
                Item.Value.bStart = true;
                if (!Item.Value.bThreadWork)
                {
                    Item.Value.bThreadWork = false;
                    try
                    {
                        if(Item.Value.threadWork!=null)
                         Item.Value.threadWork.Abort();
                    }
                    catch { }

                    if (Item.Value.bEnable)
                    {
                        Item.Value.bThreadWork = true;
                        //Item.Value.bSingleRun = true;
                        Item.Value.threadWork = new System.Threading.Thread(Item.Value.StationProcess);
                       
                        Item.Value.threadWork.Start();
                        Item.Value.threadWork.IsBackground = true;
                        Item.Value.threadWork.Priority = ThreadPriority.Highest;
                        Item.Value.threadWork.Name = Item.Value.strStationName;
                    }
                }
            }
            m_bPause = false;
            m_bPauseHive = false;
            m_bStart = true;
        }
        #endregion

        #region   StopWork  停止所有工站
        /// <summary>
        /// 停止所有工站
        /// </summary>
        public static void StopWork()
        {
            m_bPause = true;
            m_bStart = false;
            m_bPauseHive = false;
            foreach (var Item in m_stationWork)
            {
                Item.Value.bPause = true;
                Item.Value.bStart = false;
                if (Item.Value.bThreadWork)
                {
                    Item.Value.bThreadWork = false;
                    Item.Value.Realse();
                }
            }
        }
        #endregion

        #region StartWork 开始指定工站
        /// <summary>
        /// 开始指定工站
        /// </summary>
        /// <param name="_strWorkName"></param>
        public static void StartWork(string _strWorkName)
        {
            if (!m_stationWork[_strWorkName].bThreadWork)
            {
                m_stationWork[_strWorkName].bThreadWork = false;
                try
                {
                    if (m_stationWork[_strWorkName].threadWork != null)
                        m_stationWork[_strWorkName].threadWork.Abort();
                }
                catch { }

                if (m_stationWork[_strWorkName].bEnable)
                {
                    m_stationWork[_strWorkName].bThreadWork = true;
                    m_stationWork[_strWorkName].threadWork = new System.Threading.Thread(m_stationWork[_strWorkName].StationProcess);
                    m_stationWork[_strWorkName].threadWork.Start();
                    m_stationWork[_strWorkName].threadWork.IsBackground = true;
                    m_stationWork[_strWorkName].threadWork.Priority = ThreadPriority.Highest;
                    m_stationWork[_strWorkName].threadWork.Name = m_stationWork[_strWorkName].strStationName;
                }
            }
        }
        #endregion

        #region StopWork 停止指定工站
        /// <summary>
        /// 停止指定工站
        /// </summary>
        /// <param name="_strWorkName"></param>
       public static void StopWork(string _strWorkName)
        {
            m_stationWork[_strWorkName].Realse();
            m_stationWork[_strWorkName].bThreadWork = false;
            System.Threading.Thread.Sleep(100);
            try
            {
                if (m_stationWork[_strWorkName].threadWork != null)
                    m_stationWork[_strWorkName].threadWork.Abort();
            }
            catch { }

        }
        #endregion

        #region  PauseStopWork 暂停
        /// <summary>
        /// 暂停
        /// </summary>
       public static void PauseStopWork()
        {
            m_bStart = false;
            m_bPause = true;
            m_bPauseHive = false;
            foreach (var Item in m_stationWork)
            {
                Item.Value.bPause = true;
                Item.Value.bStart = false;
               
            }
        }
        #endregion

        
    }
}


